/**
 * 
 */
package ru.amse.stroganova.ui.command;

import java.awt.Point;
import java.util.HashMap;
import java.util.Map;

import ru.amse.stroganova.presentation.GraphPresentation;
import ru.amse.stroganova.presentation.VertexPresentation;
import ru.amse.stroganova.ui.layout.Layout;
import ru.amse.stroganova.ui.layout.PredefinedLayout;

class LayoutCommand implements Command {

	private final Layout newLayout;
	
	private final Layout oldLayout;
	
	private final GraphPresentation graphPresentation;
	
	/**
	 * Creates new layout command.
	 * 
	 * @param layout
	 * @param graphPresentation
	 */
	public LayoutCommand(Layout layout, GraphPresentation graphPresentation) {
		this.graphPresentation = graphPresentation;
		newLayout = layout;
		oldLayout = new PredefinedLayout(getCurrentPositions());
	}
	
	public void execute() {
		graphPresentation.pauseFiringChanges();
		newLayout.performLayout();
		graphPresentation.resumeFiringChanges();
	}
	
	public void undo() {
		graphPresentation.pauseFiringChanges();
		oldLayout.performLayout();
		graphPresentation.resumeFiringChanges();
	}
	
	private Map<VertexPresentation, Point> getCurrentPositions() {
		Map<VertexPresentation, Point> result = new HashMap<VertexPresentation, Point>();
		for (VertexPresentation vp : graphPresentation.getVertexPresentations()) {
			result.put(vp, vp.getCenter());
		}
		return result;
	}
}